package com.shanling.shanlingcontroller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;

import java.util.List;

/**
 * Created by ch1 on 2019/11/18.
 */

public class MyblutoothItemAdapter extends BaseAdapter {
    private List<MybluetoothDevice> devices;
    private Context context;

    public MyblutoothItemAdapter(List<MybluetoothDevice> devices, Context context) {
        this.devices = devices;
        this.context = context;
    }

    @Override
    public int getCount() {
        return devices.size();
    }
    public void refresh(List<MybluetoothDevice> datas){
        devices.clear();
        devices.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if(convertView==null){
            view=View.inflate(context,  R.layout.item_device,null);
            holder=new ViewHolder();
            holder.blename=view.findViewById(R.id.blename);
            view.setTag(holder);
        }else {
            view=convertView;
            holder= (ViewHolder) view.getTag();
        }
        holder.blename.setText(devices.get(position).getName());
        String bleMac = PreferenceUtil.getInstance(context).getBleMac();
        if(devices.get(position).getMac().equals(bleMac)){
            holder.blename.setTextColor(0xffCAB487);
        }else {
            holder.blename.setTextColor(0xffffffff);
        }
        return view;
    }

    static  class  ViewHolder{
        TextView blename;
    }
}
